package co.com.jtune.view.main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

import org.jdesktop.application.ResourceMap;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;


public class PlayingInfoPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private final static Color COLOR_START = Color.decode("#A6AAAF");
	private final static Color COLOR_END = Color.decode("#F5F6F7");

	private JLabel trackName;
	private JLabel trackAlbum;
	private JLabel trackArtist;
	private JSlider timePlaying;
	@SuppressWarnings("unused")
	private ResourceMap resource;

	public PlayingInfoPanel(ResourceMap resource) {
		this.resource = resource;
		
		setOpaque(true);
		
		buildComponets();
		createSimpleGui();
	}

	private void buildComponets() {
		trackName = new JLabel("...");
		trackAlbum = new JLabel("...");
		trackArtist = new JLabel("...");
		
		timePlaying = new JSlider(JSlider.HORIZONTAL);
		timePlaying.setPreferredSize(new Dimension(180, 20));
		timePlaying.setOpaque(false);
		timePlaying.setFocusable(false);
	}

	private void createSimpleGui() {
		FormLayout panelLayout = new FormLayout( "5px, pref:grow, pref:grow, 5px", "5px, pref, pref, pref, 5px");		
		setLayout(panelLayout);
		CellConstraints cc = new CellConstraints();
		
		add(trackName, cc.xywh(2, 2, 2, 1, CellConstraints.CENTER, CellConstraints.TOP));
		add(trackAlbum, cc.xywh(2, 3, 1, 1, CellConstraints.CENTER, CellConstraints.TOP));
		add(trackArtist, cc.xywh(3, 3, 1, 1, CellConstraints.CENTER, CellConstraints.TOP));
		add(timePlaying, cc.xywh(2, 4, 2, 1, CellConstraints.CENTER, CellConstraints.BOTTOM));
	}

	public void setTrackName(String trackName) {
		this.trackName.setText(trackName);
	}

	public void setTimePlaying(int timeToPlay) {
		this.timePlaying.setMinimum(0);
		this.timePlaying.setMaximum(timeToPlay);

	}
	
	@Override
	protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
		moreThanOne(g);
	}

	private void moreThanOne(final Graphics g) {
		
		int width = getWidth();
		int height = getHeight();
		
		final float[] FRACTIONS = { 0.0f, 0.6f, 1.0f };
		final Color[] DARK_COLORS = { COLOR_START, COLOR_END,
				COLOR_START };
		
		MultipleGradientPaint DARK_GRADIENT = null;
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		DARK_GRADIENT = new LinearGradientPaint(
				new Point2D.Double(0, 0), new Point2D.Double(width,
						height), FRACTIONS, DARK_COLORS);
		
		g2d.setPaint(DARK_GRADIENT);
		g2d.fillRect(0, 0, width, height);
		
	}

	@SuppressWarnings("unused")
	private void one(Graphics g) {
		int width = getWidth();
		int height = getHeight();

		// Create the gradient paint
		GradientPaint paint = new GradientPaint(0, 0, COLOR_START, width,
				height, COLOR_END, true);

		// we need to cast to Graphics2D for this operation
		Graphics2D g2d = (Graphics2D) g;

		// save the old paint
		// Paint oldPaint = g2d.getPaint();

		// set the paint to use for this operation
		g2d.setPaint(paint);

		// fill the background using the paint
		g2d.fillRect(0, 0, width, height);

		// restore the original paint
		// g2d.setPaint(oldPaint);
	}

}
